<template>
  <div class="input-box" :class="{'input-box--suffix': showSuffix}">
    <input
      class="input"
      :class="{ disabled }"
      :placeholder="placeholder"
      :type="showPassword ? (passwordVisiBle ? 'text': 'password') : type"
      :name="name"
      :disabled="disabled"
      :clearable="clearable"
      :value="value"
      @input="handleInput"
    >
    <span class="input-icon" v-if="showSuffix">
      <i class="icon-close" v-if="clearable && value" @click="sendClearInfo"></i>
      <i class="icon-eye" v-if="showPassword && !passwordVisiBle" @click="changePasswordShow"></i>
      <i class="icon-eyeClose" v-if="showPassword && passwordVisiBle" @click="changePasswordShow"></i>
    </span>
  </div>
</template>

<script>
export default {
  name: 's-input',
  data () {
    return {
      passwordVisiBle: false
    }
  },
  props: {
    placeholder: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: ''
    },
    name: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      defalut: false
    },
    value: {
      type: String,
      default: ''
    },
    clearable: {
      type: Boolean,
      defalut: false
    },
    showPassword: {
      type: Boolean,
      defalut: false
    }
  },
  methods: {
    handleInput (e) {
      this.$emit('input', e.target.value)
    },
    sendClearInfo () {
      this.$emit('input', '')
    },
    changePasswordShow () {
      this.passwordVisiBle = !this.passwordVisiBle
    }
  },
  computed: {
    showSuffix () {
      return this.clearable || this.showPassword
    }
  }
}
</script>

<style lang="scss" scoped>
.input-box {
  width: 300px;
  position: relative;
  font-size: 14px;
  display: inline-block;
  .input {
    -webkit-appearance: none;
    background-color: #fff;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    line-height: 40px;
    height: 40px;
    outline: none;
    padding: 0 15px;
    transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
    width: 100%;
    &:focus {
      color: #409eff;
      outline: none;
      border-color: #409eff;
    }
    &.disabled {
      &:hover,
      &:focus {
        color: #c0c4cc;
        outline: none;
        background-color: #f5f7fa;
        border-color: #e4e7ed;
        cursor: not-allowed;
      }
    }
  }
}

.input-box--suffix {
  .input {
    padding-right: 30px;
  }
  .input-icon {
    position: absolute;
    height: 100%;
    right: 10px;
    top: 0;
    line-height: 40px;
    text-align: center;
    color: #c0c4cc;
    transition: all .3s;
    z-index: 900;
    i {
      color: #c0c4cc;
      font-size: 14px;
      cursor: pointer;
      margin-right: 5px;
      transition: color .2s cubic-bezier(.645, .045, .355, 1);
      &:hover {
        color: #409eff;
      }
    }
  }
}
</style>
